package com.yycx.module.bus.provider.handler.haiying.settle.calculate;

import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.date.DateUtil;
import com.yycx.bpm.provider.common.CommentTypeEnum;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.module.MyExportParams;
import com.yycx.common.base.module.export.DictDataVo;
import com.yycx.common.base.module.export.ExportField;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.ExportInterceptor;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ConvertUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.entity.hy.HyOrderDetail;
import com.yycx.module.bus.provider.service.BusCustomService;
import org.flowable.engine.task.Comment;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 核算订单列表（按明细）
 *
 * @author zyf
 */
@Component
public class HyCalculateOrderTab3ListHandler implements PageInterceptor<HyOrderDetail>, ExportInterceptor<HyOrderDetail> {


    @Resource
    private BusCustomService customService;

    @Override
    public void prepare(CriteriaQuery<HyOrderDetail> cq, PageParams pageParams, EntityMap params) {
        String beginDate = cq.getParams("beginDate");
        String endDate = cq.getParams("endDate");
        String customId = cq.getParams("customId");
        String orderNo = cq.getParams("orderNo");
        String saleManName = cq.getParams("saleManName");
        Long companyId = OpenHelper.getCompanyId();
        cq.addSelect(BusDept.class, "deptName", "deptType");
        cq.addSelect(HyOrder.class, "*");
        cq.eq(BusDept.class, "deptType");
        cq.eq(HyOrder.class, "deptId");
        cq.eq(HyOrder.class, "companyId");
        if (FlymeUtils.isNotEmpty(orderNo)) {
            cq.and(e -> e.likeLeft("ho.orderNo", orderNo).or().likeLeft("ho.outOrderNo", orderNo));
        }
        if (FlymeUtils.isNotEmpty(saleManName)) {
            cq.like("ho.saleManName", saleManName);
        }
        if (FlymeUtils.isNotEmpty(beginDate)) {
            cq.ge("sendDate", beginDate);
            cq.le("sendDate", endDate);
        }
        cq.eq(FlymeUtils.isNotEmpty(customId), "customId", customId);
        cq.in(true, "ho.calculateState", 1, 2);
        cq.isNotNull("applyIncId");
        cq.eq(true, "ho.companyId", companyId);
        cq.createJoin(BusDept.class);
        cq.orderByDesc("ho.createTime");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<HyOrderDetail> cq, List<EntityMap> result, EntityMap extra) {
        String companyName = OpenHelper.getAttr("companyName", String.class);
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("applyIncId");
            Long customId = entityMap.getLong("customId");
            BusCustom custom = customService.getById(customId);
            if (FlymeUtils.isNotEmpty(custom)) {
                entityMap.put("customName", custom.getCustomName());
            }
            entityMap.put("applyIncId", processInstanceId);
            entityMap.put("companyName", companyName);
            Integer calculateState = entityMap.get("calculateState");
            if (calculateState.equals(2)) {
                entityMap.put("taskName", "已完成");
            }

        }
        return result;
    }

    @Override
    public TemplateExportParams initTemplateExportParams(Map params) {
        TemplateExportParams templateExportParams = new TemplateExportParams("excel/结算申请明细" + ".xlsx", true);
        return templateExportParams;
    }

    @Override
    public void initExportData(Map dataMap, List<EntityMap> list,CriteriaQuery<HyOrderDetail> cq) {
        dataMap.put("nickName", OpenHelper.getNickName());
        dataMap.put("applyDate", DateUtil.format(DateUtil.date(), "yyyy-MM-dd"));
        Long companyId = OpenHelper.getCompanyId();
        if (companyId.equals(5L)) {
            dataMap.put("companyName", "丹博瑞");
        }
        if (companyId.equals(1018L)) {
            dataMap.put("companyName", "赤壁医药");
        }
    }


    @Override
    public void initExcelExportEntity(ExportField exportField, List list) {
        Object key = exportField.getKey();
        if (key.equals("deptType")) {
            List<DictDataVo> dictDataVos = new ArrayList<>();
            DictDataVo dictDataVo = new DictDataVo();
            dictDataVo.setLabel("自营");
            dictDataVo.setValue("ZY");
            DictDataVo dictDataVo2 = new DictDataVo();
            dictDataVo2.setLabel("招商");
            dictDataVo2.setValue("ZS");
            dictDataVos.add(dictDataVo);
            dictDataVos.add(dictDataVo2);
            exportField.setDictData(dictDataVos);
            exportField.setReplace();
        }
        if (key.equals("sendType")) {
            List<DictDataVo> dictDataVos = new ArrayList<>();
            DictDataVo dictDataVo = new DictDataVo();
            dictDataVo.setLabel("现款");
            dictDataVo.setValue(1);
            DictDataVo dictDataVo2 = new DictDataVo();
            dictDataVo2.setLabel("预付款");
            dictDataVo2.setValue(2);
            DictDataVo dictDataVo3 = new DictDataVo();
            dictDataVo3.setLabel("资保金");
            dictDataVo3.setValue(3);
            DictDataVo dictDataVo4 = new DictDataVo();
            dictDataVo4.setLabel("资信");
            dictDataVo4.setValue(4);
            dictDataVos.add(dictDataVo);
            dictDataVos.add(dictDataVo2);
            dictDataVos.add(dictDataVo3);
            dictDataVos.add(dictDataVo4);
            exportField.setDictData(dictDataVos);
            exportField.setReplace();
        }
    }

}
